package com.ck.util;

import okhttp3.*;
import org.json.JSONObject;

import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

public class QiwuApiUtil {

    OkHttpClient client = new OkHttpClient();
    MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
    MediaType formMediaType = MediaType.parse("multipart/form-data");


    String server = "https://robot-service.centaurstech.com/api/chat";
    String appkey = "ziyou-earphone-api-test";
    String appsecret = "9502f6339f32f34f7996ea4f79bc9b8c";
    String nickname = "ck";

    String uid = "123";

    public String getResponse(String ask) throws Exception {
        String now = (new Date()).getTime() + "";
        String bodyStr = String.format(
                "appkey=%s&timestamp=%s&uid=%s&verify=%s&msg=%s&nickname=%s",
                appkey, now, uid, returnverify(now), ask, nickname);

        RequestBody body = RequestBody.create(mediaType,
                bodyStr);
        Request request = new Request.Builder()
                .url(server)
                .post(body)
                .addHeader("content-type", "application/x-www-form-urlencoded")
                .addHeader("cache-control", "no-cache")
                .build();

        Response response = client.newCall(request).execute();
        String res = response.body().string();
        System.out.println(res);

        JSONObject json = new JSONObject(res);
        if (json.has("data")) {
            System.out.println("Appended data: " + json.getJSONObject("data"));
        } else {
            System.out.println("There is no extra data");
        }
        System.out.println(json.getString("ask"));
        return json.getString("msg");
    }

    public String voiceIdentify(String filePath) throws Exception {
        String now = (new Date()).getTime() + "";
        String urlStr = String.format(
                "appkey=%s&timestamp=%s&uid=%s&verify=%s&nickname=%s&codec=wav&rate=16000",
                appkey, now, uid, returnverify(now), nickname);
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("speech", filePath,
                        RequestBody.create(MediaType.parse("multipart/form-data"), new File(filePath)))
                .build();
        Request request = new Request.Builder()
                .url("https://robot-service.centaurstech.com/api/speech/asr?"+urlStr)
                .post(requestBody)
                .addHeader("cache-control", "no-cache")
                .build();

        Response response = client.newCall(request).execute();
        String res = response.body().string();
        System.out.println(res);

        JSONObject json = new JSONObject(res);
        String msg = json.getString("msg");
        return msg;
    }


    private String returnverify(String now) throws NoSuchAlgorithmException {
        String verify = Md5.digest(appsecret + uid + now);
        return verify;
    }

    public static void main(String[] args) throws Exception {
        QiwuApiUtil demo = new QiwuApiUtil();
        demo.voiceIdentify("D:\\tmp\\1620986000226.wav");
    }
}
